import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/*@Sangeeta
 * process()=This file is used to assign a unique fid to each file. Create "n_fname_fidr"
 * process2()="Make n_fid_bugid table, that have nfid taken from  "
 */
public class uniqe_fid
{
	public String old_fid;
	public String new_fid;
	
	public Statement stmt=null;
	public Connection conn=null;
	
	public String dbname="2jboss";
	public String userid="root";
	public String passwd="123";
	
	//process
	public void process(Connection conn)
	{
		String query="SELECT distinct fname FROM fname_fdir f ";
		try 
		{
			stmt=conn.createStatement();
			stmt.execute(query);
			ResultSet rs= stmt.getResultSet();
			
			int count=1;
			while(rs.next())
			{
			  	
				Statement s2=conn.createStatement();
				String fname=rs.getString("fname");
				
				String q2="select * from fname_fdir where fname='"+fname+"'";
				s2=conn.createStatement();
				s2.execute(q2);
				ResultSet r2=s2.getResultSet();
				
				String nfid="nfid"+count;
				String fids="";
				String fdirs="";
				while(r2.next())
				{
				  	fids+=r2.getString("fid")+",";
				  	fdirs+=r2.getString("fdir")+"\n";
				}
			
				if(r2!=null)
					r2.close();
				if(s2!=null)
					s2.close();
				
				insert(nfid,fname,fids,fdirs,conn);
				
			count++;	
				
			}//while
			
			
		 if(rs!=null)
			rs.close();
		 
		 if(stmt!=null)
			 stmt.close();
		 
		}//try 
		catch (SQLException e) 
		{e.printStackTrace(); 	}
		
		
		
	}//process
	
	
public void insert(String nfid, String fname,String fids, String fdirs, Connection conn)
{
		Statement stmt = null;		
		String q="insert into n_fname_fdir values('"+nfid+"','"+fname+"','"+fids+"','"+fdirs+"')";
		try {
					stmt=conn.createStatement();
					stmt.executeUpdate(q);
					
					if(stmt!=null)
						stmt.close();
					
		} catch (SQLException e) 
		{ e.printStackTrace(); 	} 
	
		

}//insert

//to create table n_fid_bugid
public void make_n_fid_bugid(Connection conn)
{
    Statement stmt=null;
    try 
     {
		 stmt=conn.createStatement();
		 String q1="select * from fid_bugid";
		 stmt.execute(q1);
		 ResultSet r1=stmt.getResultSet();
		 
		 int count=0;
		 while(r1.next())
		 	{
			 
			 String fid=r1.getString("fid");
			 String nfid=get_nfid(fid,conn);
			
			 String bugid=r1.getString("bugid");
			 String svnid=r1.getString("svnid");
			 
			
			 
			 insert_into_n_fid_bugid(nfid, bugid,svnid, conn); 
			 
			 count++;
			 System.out.println("count="+count);
		 	}
		 
		 
		 
		 if(r1!=null)
			 r1.close();
		 
		 if(stmt!=null)
			 stmt.close();
		 
	 } catch (SQLException e) 
	 { 	e.printStackTrace(); }
    
	
}//make

public void insert_into_n_fid_bugid(String nfid, String bugid, String svnid,Connection conn)
{
  

	Statement stmt = null;		
	String q="insert into n_fid_bugid values('"+nfid+"','"+bugid+"','"+svnid+"')";
	try {
				stmt=conn.createStatement();
				stmt.executeUpdate(q);
				
				if(stmt!=null)
					stmt.close();
				
	} catch (SQLException e) 
	{ e.printStackTrace(); 	} 

	
	
	
}//


public String get_nfid(String fid, Connection conn)
{

	String nfid="";
	String q="select nfid,fids from n_fname_fdir where fids like '%"+fid+"%'";
	
	Statement stmt=null;
	try 
	  {
		 stmt=conn.createStatement();
		 stmt.execute(q);
		 ResultSet rs=stmt.getResultSet();
		 
		 boolean flag=true;
		 while(rs.next()&&flag)
		 {
			 nfid=rs.getString("nfid");
			 String fids=rs.getString("fids");
			 String temp[]=fids.split(",");
			 
			 for(int i=0;i<temp.length;i++)
			 {
				 if(fid.equalsIgnoreCase(temp[i]))
				 {
					 flag=false;
					 break;
				 }
			 }//for
		 }
		 
		 
		 if(rs!=null)
			 rs.close();
	  
		 if(stmt!=null)
				stmt.close();
	  } catch (SQLException e) 
	  { e.printStackTrace(); }
	
	
	  
	return nfid;
}

public void intr()
{

	BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
	System.out.println("INTR");
	try {
		  br.readLine();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}//intr

//main
public static void main(String args[])
{
		uniqe_fid u =  new uniqe_fid();
		db_conn db=new db_conn();
		u.conn=db.connect(u.dbname, u.userid, u.passwd);
		
		u.process(u.conn);//@ Not a comment//to create n_fname_fdir 
		//u.make_n_fid_bugid(u.conn);
		
		db.close(u.conn);
		
		
		
}

}//unique fid
